package com.sky.service.impl;

import com.sky.entity.User;
import com.sky.dao.UserDao;
import com.sky.result.LimitQueryResult;
import com.sky.result.Result;
import com.sky.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * 用户信息(User)表服务实现类
 *
 * @author lining
 * @since 2024-10-23 16:33:48
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    
    @Resource
    private UserDao userDao;

        /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result<User> queryById(Long id) {
        try {
            return new Result<>(true, this.userDao.queryById(id));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 通过主键修改单条数据【非空字段】
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public Result<User> updateById(User user) {
        try {
            this.userDao.updateById(user);
            return new Result<>(true, this.userDao.queryById(user.getId()));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 通过主键修改单条数据【所有字段】
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public Result<User> updateAllById(User user) {
        try {
            this.userDao.updateAllById(user);
            return new Result<>(true, this.userDao.queryById(user.getId()));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }

    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Integer> deleteById(Long id) {
        try {
            return new Result<>(true, this.userDao.deleteById(id));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 通过主键批量删除数据
     *
     * @param idList 主键list
     * @return 删除数量
     */
    @Override
    public Result<Integer> deleteAllByIdList(List<Long> idList){
        try {
            return new Result<>(true, this.userDao.deleteAllByIdList(idList));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 精准查询
     *
     * @param user 实例对象
     * @return 对象列表
     */
    @Override
    public Result<List<User>> queryAll(User user) {
		try {
			return new Result<>(true, this.userDao.queryAll(user));
		} catch (Exception e) {
            logger.error(e.getMessage());
			return new Result<>(false, e.getMessage());
		}
    }

    /**
     * 模糊查询
     *
     * @param user 实例对象
     * @return 对象列表
     */
    @Override
    public Result<List<User>> queryAllLike(User user) {
		try {
			return new Result<>(true, this.userDao.queryAllLike(user));
		} catch (Exception e) {
            logger.error(e.getMessage());
			return new Result<>(false, e.getMessage());
		}
    }
    
    /**
     * 分页精准查询
     *
     * @param user 实例对象
     * @param pageSize 每页数量
     * @param offset 起始索引
     * @return 对象列表
     */
     @Override
    public Result<LimitQueryResult<List<User>>> queryAllLimit(User user, int pageSize, int offset) {
		try {
		    long count = this.userDao.count(user);
		    List<User> data = this.userDao.queryAllLimit(user, pageSize, offset);
			return new Result<>(true, new LimitQueryResult<>(count, data));
		} catch (Exception e) {
            logger.error(e.getMessage());
			return new Result<>(false, e.getMessage());
		}
    }
    
    /**
     * 分页模糊查询
     *
     * @param user 实例对象
     * @param pageSize 每页数量
     * @param offset 起始索引
     * @return 对象列表
     */
     @Override
    public Result<LimitQueryResult<List<User>>> queryAllLikeLimit(User user, int pageSize, int offset) {
        try {
            long count = this.userDao.countLike(user);
		    List<User> data = this.userDao.queryAllLikeLimit(user, pageSize, offset);
			return new Result<>(true, new LimitQueryResult<>(count, data));
		} catch (Exception e) {
            logger.error(e.getMessage());
			return new Result<>(false, e.getMessage());
		}
    }
    
    /**
     * 统计总行数
     *
     * @param user 查询条件
     * @return 总行数
     */
    @Override
    public Result<Long> count(User user) {
        try {
            return new Result<>(true, this.userDao.count(user));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 统计总行数(模糊查询)
     *
     * @param user 查询条件
     * @return 总行数
     */
    @Override
    public Result<Long> countLike(User user) {
        try {
            return new Result<>(true, this.userDao.countLike(user));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public Result<User> insert(User user) {
        try {
            this.userDao.insert(user);
            return new Result<>(true, user);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 批量新增数据
     *
     * @param entities List<User> 实例对象列表
     * @return 影响行数
     */
    @Override
    public Result<Integer> insertBatch(List<User> entities) {
        try {
            int count = 0;
            for (int i = 0; i < entities.size(); i += 100) {
                count += this.userDao.insertBatch(entities.subList(i, Math.min(i + 100, entities.size())));
            }
            return new Result<>(true, count);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
    
    /**
     * 通过实体精准删除数据
     *
     * @param user 实例对象
     * @return 影响行数
     */
    @Override
    public Result<Integer> deleteAll(User user) {
        try {
            return new Result<>(true, this.userDao.deleteAll(user));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Result<>(false, e.getMessage());
        }
    }
}
